package com.hexinfo.dmpro.monitor.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hexinfo.dmpro.common.utils.StringUtils;
import com.hexinfo.dmpro.monitor.model.YarnAppModel;
import com.hexinfo.dmpro.monitor.model.base.MonitorReq;
import com.hexinfo.dmpro.monitor.model.base.MonitorRsp;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author peng
 * @date 2021/10/8 11:15
 */
@Service
public class YarnAppService extends MonitorBaseService<MonitorRsp, MonitorReq> {

    @Override
    protected MonitorReq getReqInfo(MonitorReq monitorReq) {
        String startTime = monitorReq.getStartTime();
        String endTime = monitorReq.getEndTime();
        String finishedTimeBegin = monitorReq.getFinishedTimeBegin();
        String url = monitorReq.getUrl();
        if (StringUtils.isNotBlank(monitorReq.getYarnAppId())) {
            url += "/" + monitorReq.getYarnAppId();
        } else {
            url += "?1=1&";
            try {
                if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(finishedTimeBegin)) {
                    SimpleDateFormat sdf = null;
                    if (startTime.length() == 10) {
                        sdf = new SimpleDateFormat("yyyy-MM-dd");
                    } else {
                        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    }
                    startTime = String.valueOf(sdf.parse(startTime).getTime());
                    url += "startedTimeBegin=" + startTime;
                }
                if (StringUtils.isNotBlank(finishedTimeBegin)) {
                    SimpleDateFormat sdf = null;
                    if (finishedTimeBegin.length() == 10) {
                        sdf = new SimpleDateFormat("yyyy-MM-dd");
                    } else {
                        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    }
                    finishedTimeBegin = String.valueOf(sdf.parse(finishedTimeBegin).getTime());
                    url += "finishedTimeBegin=" + finishedTimeBegin;
                }
                if (StringUtils.isBlank(startTime) && StringUtils.isBlank(finishedTimeBegin) && StringUtils.isBlank(monitorReq.getState())) {
                    url += "&" + "state=RUNNING";
                }
                if (StringUtils.isNotBlank(monitorReq.getQueue())) {
                    url += "&" + "queue=" + monitorReq.getQueue();
                }
                if (StringUtils.isNotBlank(monitorReq.getUser())) {
                    url += "&" + "user=" + monitorReq.getUser();
                }
                if (StringUtils.isNotBlank(monitorReq.getState())) {
                    url += "&" + "state=" + monitorReq.getState();
                }
                if (StringUtils.isNotBlank(monitorReq.getJobType())) {
                    url += "&" + "applicationTypes=" + monitorReq.getJobType();
                }

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        monitorReq.setUrl(url);
        return monitorReq;
    }

    @Override
    protected MonitorRsp parse(MonitorRsp monitorRsp) {
        String body = monitorRsp.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);

        if (jsonObject.getJSONObject("app") != null) {
            JSONObject app = jsonObject.getJSONObject("app");
            YarnAppModel yarnAppModel = JSONObject.parseObject(JSONObject.toJSONString(app),YarnAppModel.class);
            yarnAppModel.setStartedTime(dealDate(yarnAppModel.getStartedTime()));
            yarnAppModel.setFinishedTime(dealDate(yarnAppModel.getFinishedTime()));
            yarnAppModel.setLaunchTime(dealDate(yarnAppModel.getLaunchTime()));
            monitorRsp.setObj(yarnAppModel);
        }

        if (jsonObject.getJSONObject("apps") != null) {
            JSONArray apps = jsonObject.getJSONObject("apps").getJSONArray("app");
            List<YarnAppModel> yarnAppModelList = JSONObject.parseArray(JSONObject.toJSONString(apps), YarnAppModel.class);
            if (yarnAppModelList != null && yarnAppModelList.size() > 0) {
                for (int i = 0; i < yarnAppModelList.size(); i++) {
                    YarnAppModel yarnAppModel = yarnAppModelList.get(i);
                    yarnAppModel.setStartedTime(dealDate(yarnAppModel.getStartedTime()));
                    yarnAppModel.setFinishedTime(dealDate(yarnAppModel.getFinishedTime()));
                    yarnAppModel.setLaunchTime(dealDate(yarnAppModel.getLaunchTime()));
                }
            }
            monitorRsp.setObj(yarnAppModelList);
        }
        return monitorRsp;
    }


    private String dealDate(String time) {
        if (StringUtils.isBlank(time) || time.equals("0")) {
            return time;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(Long.parseLong(time));
    }

    @Override
    protected void saveInfo(MonitorRsp monitorRsp) {
    }

}